package com.sy.jtabytetcc.service;

import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sy.jtabytetcc.dao.RepoAReposity;
import com.sy.jtabytetcc.pojo.RepoA;

@Service("repoAServiceConfirm")
public class RepoAServiceConfirm implements IRepoAService {
	@Autowired
	private RepoAReposity repoAReposity;
//	@Autowired
//	private JdbcTemplate jdbcTemplate;

	@Transactional
	@Override
	public void tradingItems(Integer id,Integer targetRepoId,  Integer count) {
		Optional<RepoA> repoAOpt = repoAReposity.findById(id);
		RepoA repoA = repoAOpt.orElse(new RepoA());
		//将冻结的恢复
		repoA.setFreeze((repoA.getFreeze()-count)>=0?(repoA.getFreeze()-count):0);
		//减去库存
		repoA.setStocks(repoA.getStocks()-count);
		repoAReposity.save(repoA);
//		jdbcTemplate.update("update repo_a set freeze=freeze-?,stocks=stocks-? where id=?",count,count,id);
	}

}
